//q4.cpp
//This program takes a number and set of digits from the user, checks whether the number is unique 
//and how many times the digit appear in that number
//
//Binyamin 4 Jan 2018

#include<iostream>

using namespace std;

const int NEGATIVE_INPUT_NUMBER = -1;
const int ILLEGAL_DIGIT = -2;

//count_digit
//input: number and a digit
//output: how many appearances of num in digits.
int count_digit(int num, int digit)
{
	int count;
	count = 0;
	if((digit<0)||(digit>=10))
		return ILLEGAL_DIGIT;
	if(num<=0)
		return NEGATIVE_INPUT_NUMBER;
	while(num>0)
	{
		if(num%10 == digit) count++;
		num = num/10;
	}
	return count;

}
//is_unique
//input: a number.
//output: are there repeating digits in it.
bool is_unique(int num)
{
	while(num>0)
	{
		if(count_digit(num,num%10)>1) return false;
		num = num/10;
		
	}
	return true;
}
//add_digits
//input: a number and a set of digits.
//output: how many times each of the digits appear in the number.
int add_digits(int num, int digit)
{
	int digit_count_sum;
	digit_count_sum = 0;
	if((digit < 0) || !(is_unique(digit)))
		return ILLEGAL_DIGIT;
	if(num<0)
		return NEGATIVE_INPUT_NUMBER;
	while(digit>0)
	{
		digit_count_sum += count_digit(num,digit%10);
		digit = digit/10;
	}
	return digit_count_sum;
}

//1. prompt for a num and a digit
//2. store the num and the digit.
//3. calculate whether the number is unique
//4. calculate how many times the digits appear in the number
int main()
{
	int num, digit; //num, digit - intput from the user
	cout << "Please enter a number ";
	cin >> num;
	cout << "Please enter a digit ";
	cin >> digit;
	if(is_unique(num)) cout << "The number is unique" << endl;
	else cout << "The number is not unique" << endl;
	cout << "The digit " << digit << " appears " << add_digits(num,digit) <<" times at " << num << endl;
	return 0;
}
//output
//Please enter a number 122423
//Please enter a digit 21 
//The number is not unique
//The digit 21 appears 4 times at 122423
